/**
 * Created by zhouwei on 2017/4/21.
 */
import api from '../common/index'
import helper from '../common/helper'
import config from '../config/index'

//获取某个县的镇列表


function getUser(success) {
    let user = helper.localStorage('userMsg');
    let prvilegeLevel = user.PrvilegeLevel;
    if(prvilegeLevel == 1){
        // var d = helper.localStorage('TownUser'); // 先读缓存
        // if(d) {
        //     success(d)
        //     return
        // }
        let params = { county_id: user.CountyId,offset:0,number:config.site.number};
        api.goverment.town_user(params,function (data) {
            helper.localStorage('TownUser',data);
            success(data);
        })
    }else if(prvilegeLevel == 2){
        // var d = helper.localStorage('VillageUser'); // 先读缓存
        // if(d) {
        //     success(d)
        //     return
        // }
        let params = { town_id: user.TownId,offset:0,number:config.site.number}
        api.goverment.village_user(params,function (data) {
            helper.localStorage('VillageUser',data);
            success(data);
        })
    }else{
        // var d = helper.localStorage('FarmerUser'); // 先读缓存
        // if(d) {
        //     success(d)
        //     return
        // }
        let params = { village_id: user.VillageId,offset:0,number:config.site.number}
        api.goverment.village_farmer(params,function (data) {
            helper.localStorage('FarmerMsg',data);
            success(data);
        })
    }
}

function addUser(Name,Phoneno,RegionId,RegionName,success) {
    let user = helper.localStorage('userMsg');
    let prvilegeLevel = user.PrvilegeLevel;
    if(prvilegeLevel == 1)
    {
        let params = {Id:0,Name:Name,Phoneno:Phoneno.toString(),RegionId:parseInt(RegionId),RegionName:RegionName}
        api.goverment.add_town_user(params,function (data) {
            success(data);
        })
    }
    else if(prvilegeLevel == 2)
    {
        let params = {Id:0,Name:Name,Phoneno:Phoneno.toString(),RegionId:parseInt(RegionId),RegionName:RegionName}
        api.goverment.add_village_user(params,function (data) {
            success(data);
        })
    }
    else
    {

    }
}

function updateUser(Id,Name,Phoneno,RegionId,RegionName,success) {
    let user = helper.localStorage('userMsg');
    let prvilegeLevel = user.PrvilegeLevel;
    if(prvilegeLevel == 1)
    {
        let params = {Id:parseInt(Id),Name:Name,Phoneno:Phoneno.toString(),RegionId:parseInt(RegionId),RegionName:RegionName}
        api.goverment.update_town_user(params,function (data) {
            success(data);
        })
    }
    else if(prvilegeLevel == 2)
    {
        let params = {Id:parseInt(Id),Name:Name,Phoneno:Phoneno.toString(),RegionId:parseInt(RegionId),RegionName:RegionName}
        api.goverment.update_village_user(params,function (data) {
            success(data);
        })
    }
    else
    {
        let params = {Id:parseInt(Id),Name:Name,Phoneno:Phoneno.toString(),RegionId:parseInt(RegionId),RegionName:RegionName}
        api.goverment.update_town_user(params,function (data) {
            success(data);
        })
    }
}
function readFarmerMsgWithId(Id,success) {
    let deslist = [];
    let farmerlist = helper.localStorage('FarmerMsg');
    for(let i in farmerlist){
        if(farmerlist[i].Id == Id){
            deslist = farmerlist[i];
        }
    }
    success(deslist);
}

export default {
    goverUser:function (success) {
        getUser(function (data) {
            success(data);
        });
    },
    addGoverUser:function (Name,Phoneno,RegionId,RegionName,success) {
        addUser(Name,Phoneno,RegionId,RegionName,success)
    },
    updateGoverUser:function (Id,Name,Phoneno,RegionId,RegionName,success) {
        updateUser(Id,Name,Phoneno,RegionId,RegionName,success)
    },
    getFarmerMsg:function (Id,success) {
        readFarmerMsgWithId(Id,function (data) {
            success(data);
        })
    },
    agreeFarmerAuth:function(Id,success){
        let params = {Id:parseInt(Id)};
        api.goverment.agree_farmer_auth(params,success);
    },
    refuseFarmerAuth:function (Id,success) {
        let params = {Id:parseInt(Id)};
        api.goverment.refuse_farmer_auth(params,success);
    }
}